package com.chuangjiangx.member.manager.common;

import com.chuangjiangx.member.manager.basic.web.request.Page;
import com.chuangjiangx.member.manager.basic.web.response.PageResponse;
import com.chuangjiangx.member.manager.basic.web.response.Response;
import com.chuangjiangx.partner.platform.common.basic.PagingResult;

import java.util.List;
import java.util.Objects;

/**
 * 响应数据工具类.
 */
@SuppressWarnings("All")
public class ResponseUtils {

    /**
     * 返回成功响应.
     */
    public static Response success() {
        return success(null);
    }

    /**
     * 返回成功响应.
     *
     * @param data 数据
     * @return 响应对象
     */
    public static Response success(Object data) {
        Response response = new Response();
        response.setSuccess(true);
        response.setData(data);
        return response;
    }

    /**
     * 返回成功响应.
     *
     * @param dataName 数据对象名称
     * @param data     数据对象
     * @return 响应对象
     */
    public static Response success(Response response, String dataName, Object data) {
        if (response == null) {
            response = new Response();
        }
        response.setSuccess(true);
        response.putData(dataName, data);
        return response;
    }

    /**
     * 返回成功响应.
     *
     * @param dataName 数据对象名称
     * @param data     数据对象
     * @return 响应对象
     */
    public static Response success(String dataName, Object data) {
        return success(new Response(), dataName, data);
    }

    /**
     * 返回有分页的成功响应.
     *
     * @param page     分页对象
     * @param dataName 数据对象名称
     * @param data     数据对象
     * @return 响应对象
     */
    public static <T> PageResponse successPage(Page page, PagingResult pagingResult, String dataName, List<T> data) {
        PageResponse pageResponse = new PageResponse();

        Objects.requireNonNull(page).setTotalCount(pagingResult.getTotal());

        pageResponse.setPage(page);
        pageResponse.setSuccess(true);
        pageResponse.putData(dataName, data);
        return pageResponse;
    }

    /**
     * 返回错误的响应.
     *
     * @param errorCode 错误编码
     * @param errorMsg  错误信息
     * @return 响应对象
     */
    public static Response error(String errorCode, String errorMsg) {
        Response response = new Response();
        response.setSuccess(false);
        response.setErr_code(errorCode);
        response.setErr_msg(errorMsg);
        return response;
    }
}
